const dateFormat = require('./util/date.js');

module.exports = async function(page,url = 'https://onlyfans.com/yogasexpositions') {
    
    console.log(url,"url");
    
    try {
        await page.goto(url);
        await page.waitFor(6000);
        await page.waitFor('.g-user-name.m-lg-size');
    } catch (error){
        console.log(error)
        return {
            url,
            "nickName":"page Not Fount",
            "posts":"",
            "likes":"",
            "lastSeen":"",
            "userInfo":"",
            "avatar":"",
            "Date": dateFormat(new Date())
        }
    }
    
    
    const nickNameHandle = await page.$('.g-user-name.m-lg-size');
    let nickName = await page.evaluate(body => body.innerHTML, nickNameHandle);
    nickName = nickName.split(' <!---->')[0].replace('&amp;','&').split('<svg class="g-icon m-verified" aria-hidden="true"><use xlink:href="#icon-verified" href="#icon-verified"></use></svg>')[0];
    console.log(nickName, "nickName")
    
    let posts =  0;
    try {
        let postsHandle = await page.$('.b-profile__sections__link .b-profile__sections__count');
        posts =  await page.evaluate(body => body.innerHTML, postsHandle);
        console.log(posts,"posts")
    } catch (error) {
        
    }
   
    let likes = 0;
    try {
        let likesHandle = await page.$('.b-profile__sections__link.m-no-hover .b-profile__sections__count');
        likes = await page.evaluate(body => body.innerHTML, likesHandle);
        console.log(likes,"likes")
    } catch (error) {
        
    }
    
    let lastSeen = ''
    try {
        let lastSeenHandle = await page.$('.b-profile__user__status.m-separator.m-nowrap .b-profile__user__status__text span');
        lastSeen = await page.evaluate(body => body.innerHTML, lastSeenHandle);
    } catch (error) {
        console.log(lastSeen, "lastSeen")
    }
    
    let userInfo = '';
    try {
        await page.click(".b-user-info__collapsed-link.g-btn.m-flat")
        let userInfoHandle = await page.$('.b-user-info__content.m-collapsed');
        userInfo =  await page.evaluate(body => body.innerHTML, userInfoHandle);
    } catch (error) {
        console.log(userInfo,"userInfo")
    }

    let avatarHandle = await page.$('.g-avatar.online_status_class.m-w150.m-pointer img')
    const avatar = await page.evaluate(img => img.src, avatarHandle);
    console.log(avatar,"avatar");

   
    return {
        url,
        nickName,
        posts,
        likes,
        lastSeen,
        userInfo,
        avatar,
        "Date": dateFormat(new Date())
    }

  };